import requests

API_KEY = "X8fBs1RIRBXBznG2xdHqou8B"
SECRET_KEY = "PDE0jxtRLfLLcrjPPmOXgjh0jGVI2mxl"

def TextOCR(image):
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
    # 二进制方式打开图片文件
    params = {"image":image}
    access_token = get_access_token()
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    return response.json()
def get_access_token():
    API_KEY = "X8fBs1RIRBXBznG2xdHqou8B"
    SECRET_KEY = "PDE0jxtRLfLLcrjPPmOXgjh0jGVI2mxl"
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))
